<?php

namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function _initialize()
    {
        if (empty($_SESSION['empid'])) {

            //$this->error('页面加载中。。。',U('Login/Login'),1);
            $this->redirect('Login/Login');

        }

    }

    public function Index()
    {

        if (IS_POST) {
            $star_time = I('post.start_time');
            $end_time = I('post.end_time');
            $d = strtotime($star_time);
            $en = strtotime($end_time);
//            dump($d);exit;
//            if(!empty($d) && !empty($en) && $d > strtotime(date('Y-m-d'))){
//                $this->error('只能查询当前周和当前周以前的数据1');
//            }
//            if(!empty($en) && !empty($d) && $en > strtotime(date('Y-m-d'))){
//                $this->error('只能查询当前周和当前周以前的数据');
//            }
            if ($d > $en or empty($d) or empty($en)) {
                $this->error('时间区间选择有误');
            } else {
                $ts = (($en - $d) / 86400) + 1;
                for ($i = 0; $i < $ts; $i++) {
                    $riqi = date('Y-m-d', $d);
                    $star_time = date('Y-m-d', $d);
                    $end_time = date('Y-m-d', $d);
                    $map['start_time'] = array('between', '' . $star_time . ',' . $end_time . '');
                    $sq[$riqi] = M('CCourse')->where($map)->select();
                    foreach ($sq[$riqi] as $k => $v) {
                        $map['id'] = $v['schedule_id'];
                        $sj = M('CSchedule')->where($map)->field('schename,dteacher,jteacher')->find();
                        $sq[$riqi][$k]['schename'] = $sj['schename'];
                        $sq[$riqi][$k]['dteacher'] = $sj['dteacher'];
                        $sq[$riqi][$k]['jteacher'] = $sj['jteacher'];
                    }
                    $d += 86400;
                }
            }

        } else {

            if (I('get.sd') != '') {
                if (I('get.fs') == 'down') {
                    $sd1 = I('get.sd');
//                    dump($sd1);exit;
                    $sd = $sd1 + (7 * 24 * 60 * 60);
                } elseif (I('get.fs') == 'up') {
                    $sd1 = I('get.sd');
                    $sd = $sd1 - (7 * 24 * 60 * 60);

                } else {
                    $week = date('w');
                    if ($week == 1) {
                        $sd = strtotime(date('Y-m-d'));
                    } else {
                        $sd = strtotime('last monday');
                    }
                }
            } else {
                $week = date('w');
                if ($week == 1) {
                    $sd = strtotime(date('Y-m-d'));
                } else {
                    $sd = strtotime('last monday');
                }
            }
            //当前日期
            $sdnow = strtotime(date('Y-m-d'));
            //周一日期
            $sdddd = date('Y-m-d', $sd);
//            周日日期
            $sdddd2 = $sd + (6 * 24 * 60 * 60);
            $this->assign('sd', $sd);
            $this->assign('sdddd', $sdddd);
            $this->assign('sdddd2', $sdddd2);
            $this->assign('sdnow', $sdnow);
            $d = $sd;
            header("Content-type:text/html;charset=utf-8");
//            $map['id'] = 1081;
//            $sj = M('CSchedule')->where($map)->field('schename,dteacher,jteacher')->find();
//            dump($sj);exit;
            for ($i = 0; $i < 7; $i++) {
                $riqi = date('Y-m-d', $d);
                $star_time = date('Y-m-d', $d);
                $end_time = date('Y-m-d', $d);
                $map['start_time'] = array('between', '' . $star_time . ',' . $end_time . '');
                $sq[$riqi] = M('CCourse')->join('gb_c_schedule ON gb_c_course.schedule_id = gb_c_schedule.id')
                                         ->where($map)
                    ->field('gb_c_schedule.schename,gb_c_schedule.dteacher,gb_c_schedule.jteacher,gb_c_course.id,gb_c_course.schedule_id,
                    gb_c_course.courseid,gb_c_course.coursename,gb_c_course.start_time,gb_c_course.teacher,gb_c_course.lesson,gb_c_course.class,
                    gb_c_course.number,gb_c_course.money,gb_c_course.remark,gb_c_course.crc,is_am')
                    ->select();
//                $sq[$riqi] = M('CCourse')->where($map)->select();
//                foreach ($sq[$riqi] as $k => $v) {
//                    $map['id'] = $v['schedule_id'];
//                    $sj = M('CSchedule')->where($map)->field('schename,dteacher,jteacher')->select();
//                    $sq[$riqi][$k]['schename'] = $sj['schename'];
////                    $sq[$riqi][$k]['dteacher'] = $sj['dteacher'];
////                    $sq[$riqi][$k]['jteacher'] = $sj['jteacher'];
//                }
                $d += 86400;
            }
        }
//        dump($sq);exit;
        $class = R('Pub/Classs');
        $teacher = R('Pub/Teacher');
//        dump($teacher);exit;
        $this->assign('teacher', $teacher);
        $this->assign('class', $class);
//var_dump($sq);
        $this->assign('sq', $sq);
        $this->assign('riqi', $riqi);
        $this->display('index');

    }

    public function index2()
    {    //获取周一的日期
        // var_dump($_SESSION);
        if (I('get.fs') == 'down') {
            $start = strtotime(I('get.sd')) + (7 * 24 * 60 * 60);
            $sd = date('Y-m-d', $start);
            $end = $start + (6 * 24 * 60 * 60);
            $ts = 7;
        } elseif (I('get.fs') == 'up') {
            $start = strtotime(I('get.sd')) - (7 * 24 * 60 * 60);
            $sd = date('Y-m-d', $start);
            $end = $start + (6 * 24 * 60 * 60);
            $ts = 7;
        } else {
            if (IS_POST) {
                //查询条件
                $start_time = I('post.start_time');
//                dump($start_time);
                $end_time = I('post.end_time');
//                dump($end_time);exit;
                if ($start_time > $end_time or empty($start_time) or empty($end_time)) {
                    $this->error('时间区间选择有误');
                }
                $start = strtotime($start_time);
                $end = strtotime($end_time);
                $ts = (($end - $start) / 86400) + 1;
            } else {
                $week = date('w');
                if ($week == 1) {
                    $start_time = date('Y-m-d');
                } else {
                    $start_time = date('Y-m-d', strtotime('last monday'));
                }
                $start = strtotime($start_time);
                $sd = $start_time;
                $end = $start + 86400*6;
                $ts = 7;
            }
        }
        $start_time = date('Y-m-d', $start);
        $end_time = date('Y-m-d', $end);
        //获取课程
        for ($i = 0; $i < $ts; $i++) {
            $riqi = date('Y-m-d', $start);
            $map['start_time'] = $riqi;
            $arrCourse[$riqi] = M('CCourse')->where($map)->order('start_time asc')->select();
//            //求出每天班次数 赋值给num
//            $a = M('CCourse')->where($map)->distinct(true)->field('schedule_id')->select();
//            $arrCourse[$riqi][num] = count($a);
            //求出每天人数/单间数/标间数
            //-------------------------------
            $a = M('CCourse')->where($map)->distinct(true)->field('schedule_id,start_time,courseid')->select();
             $peopleNum = 0;
            $singleRoomNum = 0;
            $standardRoomNum = 0;
            foreach ($a as $item => $v2){
                $skc=M('CCourse')->where(array('courseid'=>$v2['courseid']))->order('start_time desc')->field('start_time')->find();
               // var_dump($skc);
               if($skc['start_time']!==$v2['start_time']){
               
                 $id['id'] = $v2['schedule_id'];
                 $peopleNum += M('CSchedule')->where($id)->getField('peoplenum');

                 $singleRoomNum += M('CSchedule')->where($id)->getField('singleroomnum');
                 $standardRoomNum += M('CSchedule')->where($id)->getField('standardroomnum');
              }  
            }
            // foreach ($a as $item => $v2){
            //     $a[] = $v2['schedule_id'];
            // }
            // $a = implode(',', $a);
            // $peopleNum = 0;
            // $singleRoomNum = 0;
            // $standardRoomNum = 0;
            // if($a) {
            //     $id['id'] = array('in', $a);
            //     $peopleNum = M('CSchedule')->where($id)->sum('peoplenum');
            //     $singleRoomNum = M('CSchedule')->where($id)->sum('singleroomnum');
            //     $standardRoomNum = M('CSchedule')->where($id)->sum('standardroomnum');
            // }
            $arrCourse[$riqi][peoplenum] = $peopleNum;
            $arrCourse[$riqi][singleroomnum] = $singleRoomNum;
            $arrCourse[$riqi][standardroomnum] = $standardRoomNum;
            //-------------------------------------
            $start += 86400;
        }
        //获取班次
        $where['start_time'] = array('between', '' . $start_time . ',' . $end_time . '');
        $ba = M('CCourse')->where($where)->order('start_time asc')->Distinct(true)->select();
        foreach ($ba as $item => $value) {
            $m[] = $value['schedule_id'];
        }
        $m = implode(',', $m);
        $arrSchedule = array();
        if($m) {
            $id['id'] = array('in', $m);
            $arrSchedule = M('CSchedule')->where($id)->order('startdate asc,noontype1 asc ')->select();
            //人数
            $sumPeople = M('CSchedule')->where($id)->field('SUM(peoplenum)')->select();
            //单间数
            $sumSingle = M('CSchedule')->where($id)->field('SUM(singleroomnum)')->select();
            //标间数
            $sumStandard = M('CSchedule')->where($id)->field('SUM(standardroomnum)')->select();
        }
        header("Content-type:text/html;charset=utf-8");
//        dump($arrCourse);exit;
        $this->assign('start_time', $start_time);
        $this->assign('sd', $sd);
        $this->assign('end_time', $end_time);
        $this->assign('arrSchedule', $arrSchedule);
//        dump($arrCourse);exit;
        $this->assign('arrCourse', $arrCourse);
        $this->assign('sumPeople', $sumPeople);
        $this->assign('sumSingle', $sumSingle);
        $this->assign('sumStandard', $sumStandard);
        $this->display();
    }

    public function header()
    {

        $user = M('sys_user');
        $map['empid'] = session('empid');
        $usinfo = $user->where($map)->find();

        return $usinfo;
        $this->display();

    }

    public function left()
    {

        $this->display();

    }

    public function kcshow()
    {
        if (IS_AJAX) {
            $map['coursename'] = I('post.kcname');
            $map['id'] = I('post.bh');
            $kc = M('CCourse')->where($map)->find();
//            dump();exit;
            $this->assign('kc', $kc);
            $this->display();
        }

    }

    public function bcshow()
    {
        if (IS_AJAX) {
//            var_dump(I('post.bcmc'));exit;
            $map['id'] = I('post.bcmc');
            $scheid = M('CSchedule')->where($map)->find();
            $where['classid'] = $scheid['scheid'];
            $student = M('CStudent')->where($where)->select();
            $mas['schedule_id'] = I('post.bcmc');
            $kc = M('CCourse')->where($mas)->order('start_time asc,id asc')->select();
            $this->assign('kecheng', $kc);
            $this->assign('sche', $scheid);
            $this->assign('student', $student);
//             var_dump($kc);exit;
            $this->display();
        }


    }

    public function xgmm()
    {
        if (IS_POST) {
            $opassword = MD5($_POST['opassword']);
            $npassword = MD5(I('post.password'));
            $map['loginname'] = I('post.loginname');
            $password = M('SysUser')->where($map)->getField('password');
            if ($opassword == $password) {
                $data['password'] = $npassword;
                $data['zt'] = 1;
                $up = M('SysUser')->where($map)->save($data);
                if ($up) {
                    $data = 0;

                } else {

                    $data = 1;
                }

            } else {
                $data = 3;

            }

            echo $data;
        }


    }

    public function saveclass()
    {
        $map['id'] = I('post.id');
        $data['class'] = I('post.value');
        $up = M('CCourse')->where($map)->save($data);
        if ($up) {
            $data = 0;

        } else {
            $data = 1;

        }
        echo $data;


    }

    public function savedteacher()
    {
        $map['scheid'] = I('post.id');
        $data['dteacher'] = I('post.value');
        $up = M('CSchedule')->where($map)->save($data);
        if ($up) {
            $data = 0;

        } else {
            $data = 1;

        }
        echo $data;


    }

    public function savejteacher()
    {
        $map['scheid'] = I('post.id');
        $data['jteacher'] = I('post.value');
        $up = M('CSchedule')->where($map)->save($data);
        if ($up) {
            $data = 0;

        } else {
            $data = 1;

        }
        echo $data;


    }
}